import { loginAPI } from '@/api/user'
import { getToken, setToken } from '@/utils/auth'
export default {
  state: {
    token: getToken() || ''
  },
  mutations: {
    SET_TOKEN (state, token) {
      state.token = token
      // token持久化
      setToken(token)
    },
    REMOVE_TOKEN (state) {
      state.token = ''
      // 移除持久化
      setToken('')
    }
  },
  actions: {
    // 登录
    async login (context, { username, password }) {
      const res = await loginAPI({ username, password })
      context.commit('SET_TOKEN', res.data.token)
    }
  }
}
